<template>
  <div id="app">
    <!-- <div id="nav">
      <router-link to="/">Home</router-link> |
      <router-link to="/about">About</router-link> |
      <router-link to="/left">Left</router-link>
    </div> -->
    <div>
      <span>请输入要查询的cityCode: </span>
      <input style="width: 80px;"
             v-model="cityCode"
             placeholder="请输入要查询的城市代码"
             type="text">
    </div>
    <button @click="getWeatherData">查询</button>
    <router-view />
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      cityCode: '', // 110105
      // 查询数据结果
      dataList: {
        city: '',
        humidity: '',
        province: '',
        reporttime: '',
        temperature: '',
        weather: '',
        winddirection: '',
        windpower: ''
      }
    }
  },
  methods: {
    getWeatherData () {
      if (!this.cityCode) {
        console.log('请输入要查询的城市代码')
        return
      }
      axios.get(`https://restapi.amap.com/v3/weather/weatherInfo?key=475eb6416a2c7b780cd6e19023b138dc&city=${this.cityCode}`)
        .then(res => {
          // console.log(res)
          const infomations = res.data.lives[0]
          this.dataList.city = infomations.city
          this.dataList.humidity = infomations.humidity
          this.dataList.province = infomations.province
          this.dataList.reporttime = infomations.reporttime
          this.dataList.temperature = infomations.temperature
          this.dataList.weather = infomations.weather
          this.dataList.winddirection = infomations.winddirection
          this.dataList.windpower = infomations.windpower
          throw new Error('你没有输入查询城市的代码哦~')
        }).catch(() => {
          console.log('出错了')
        })
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

#nav {
  padding: 30px;
}

#nav a {
  font-weight: bold;
  color: #2c3e50;
}

#nav a.router-link-exact-active {
  color: #42b983;
}
</style>
